Trò chơi đua xe động vật trong UNITY Engine
114.615 lượt xem;
1 using UnityEngine;
2 using System.Collections;
3
4 public class ActionColorTo : TemporalAction {
5
6 private float endRed;
7 private float endGreen;
8 private float endBlue;
9 private float endAlpha;
10
11 private float startRed;
12 private float startGreen;
13 private float startBlue;
14 private float startAlpha;
15
16 public ActionColorTo(Color color, float duration, Interpolation interpolation) {
17 endRed = color.r;
18 endGreen = color.g;
19 endBlue = color.b;
20 endAlpha = color.a;
21 SetDuration(duration);
22 SetInterpolation(interpolation);
23 }
24 public ActionColorTo(float r, float g, float b, float duration, Interpolation interpolation) {
25 endRed = r;
26 endGreen = g;
27 endBlue = b;
28 endAlpha = 1;
29 SetDuration(duration);
30 SetInterpolation(interpolation);
31 }
32 public ActionColorTo(float r, float g, float b, float a, float duration, Interpolation interpolation) {
33 endRed = r;
34 endGreen = g;
35 endBlue = b;
36 endAlpha = a;
37 SetDuration(duration);
38 SetInterpolation(interpolation);
39 }
40 public ActionColorTo(Color color, float duration)
41 {
42 endRed = color.r;
43 endGreen = color.g;
44 endBlue = color.b;
45 endAlpha = color.a;
46 SetDuration(duration);
47 }
48 public ActionColorTo(float r, float g, float b, float duration)
49 {
50 endRed = r;
51 endGreen = g;
52 endBlue = b;
53 endAlpha = 1;
54 SetDuration(duration);
55 }
56 public ActionColorTo(float r, float g, float b, float a, float duration)
57 {
58 endRed = r;
59 endGreen = g;
60 endBlue = b;
61 endAlpha = a;
62 SetDuration(duration);
63 }
64
65 protected override void begin()
66 {
67 Color color = actor.gameObject.GetComponent<SpriteRenderer>().color;
68 startRed = color.r;
69 startGreen = color.g;
70 startBlue = color.b;
71 startAlpha = color.a;
72 }
73
74 protected override void end()
75 {
76 }
77
78 protected override void UpdateAction(float percent)
79 {
80 actor.gameObject.GetComponent<SpriteRenderer>().color = new Color(
81 calculate(startRed, endRed, percent),
82 calculate(startGreen, endGreen, percent),
83 calculate(startBlue, endBlue, percent),
84 calculate(startAlpha, endAlpha, percent));
85 }
86
87 private float calculate(float start_value, float end_value, float percent)
88 {
89 return start_value + (end_value - start_value) * percent;
90 }
91 }
2 using System.Collections;
3
4 public class ActionColorTo : TemporalAction {
5
6 private float endRed;
7 private float endGreen;
8 private float endBlue;
9 private float endAlpha;
10
11 private float startRed;
12 private float startGreen;
13 private float startBlue;
14 private float startAlpha;
15
16 public ActionColorTo(Color color, float duration, Interpolation interpolation) {
17 endRed = color.r;
18 endGreen = color.g;
19 endBlue = color.b;
20 endAlpha = color.a;
21 SetDuration(duration);
22 SetInterpolation(interpolation);
23 }
24 public ActionColorTo(float r, float g, float b, float duration, Interpolation interpolation) {
25 endRed = r;
26 endGreen = g;
27 endBlue = b;
28 endAlpha = 1;
29 SetDuration(duration);
30 SetInterpolation(interpolation);
31 }
32 public ActionColorTo(float r, float g, float b, float a, float duration, Interpolation interpolation) {
33 endRed = r;
34 endGreen = g;
35 endBlue = b;
36 endAlpha = a;
37 SetDuration(duration);
38 SetInterpolation(interpolation);
39 }
40 public ActionColorTo(Color color, float duration)
41 {
42 endRed = color.r;
43 endGreen = color.g;
44 endBlue = color.b;
45 endAlpha = color.a;
46 SetDuration(duration);
47 }
48 public ActionColorTo(float r, float g, float b, float duration)
49 {
50 endRed = r;
51 endGreen = g;
52 endBlue = b;
53 endAlpha = 1;
54 SetDuration(duration);
55 }
56 public ActionColorTo(float r, float g, float b, float a, float duration)
57 {
58 endRed = r;
59 endGreen = g;
60 endBlue = b;
61 endAlpha = a;
62 SetDuration(duration);
63 }
64
65 protected override void begin()
66 {
67 Color color = actor.gameObject.GetComponent<SpriteRenderer>().color;
68 startRed = color.r;
69 startGreen = color.g;
70 startBlue = color.b;
71 startAlpha = color.a;
72 }
73
74 protected override void end()
75 {
76 }
77
78 protected override void UpdateAction(float percent)
79 {
80 actor.gameObject.GetComponent<SpriteRenderer>().color = new Color(
81 calculate(startRed, endRed, percent),
82 calculate(startGreen, endGreen, percent),
83 calculate(startBlue, endBlue, percent),
84 calculate(startAlpha, endAlpha, percent));
85 }
86
87 private float calculate(float start_value, float end_value, float percent)
88 {
89 return start_value + (end_value - start_value) * percent;
90 }
91 }